package com.goldcn.order.controller;

import com.goldcn.common.ExportExcel;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.config.ProConstants;
import com.goldcn.order.dto.UserCommissionDto;
import com.goldcn.order.service.CommissionUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wanglq on 2017/6/6.
 */
@Api(value = "佣金服务")
@RestController
@RequestMapping(value = "/commissionUser")
public class CommissionUserController {
    @Autowired
    CommissionUserService commissionUserService;
    @Autowired
    RestTemplate restTemplate;

    @ApiOperation(value = "我的奖金")
    @RequestMapping(value = "/{userID}", method = RequestMethod.GET)
    public Object getUserCommissionDetail(@PathVariable Long userID) {
        return commissionUserService.getUserCommissionDetail(userID);
    }

    @ApiOperation(value = "我的积分")
    @RequestMapping(value = "/integral/{userID}", method = RequestMethod.GET)
    public Object getUserCommissionDetail2(@PathVariable Long userID) {
        return commissionUserService.getUserCommissionDetail2(userID);
    }

    @ApiOperation(value = "按条件查询分销商/店铺/员工的佣金列表信息")
    @RequestMapping(value = "/query/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public Object queryCommissionListCondition(@PathVariable int pageNum, @PathVariable int pageSize, @RequestBody UserCommissionDto selectDto) {
        return commissionUserService.selectShopListByCondition(pageNum, pageSize, selectDto);
    }

    @RequestMapping(value = "/devOpt", method = RequestMethod.POST)
    public Object devOpt(@RequestBody Map<String, Object> map) {
        return commissionUserService.handErrorOrder(MapUtils.getString(map, "ids"));
    }


    @ApiOperation(value = "奖金详情")
    @RequestMapping(value = "/detail/{detailID}", method = RequestMethod.GET)
    public Object getCommissionDetail(@PathVariable Long detailID) {
        return commissionUserService.getCommissionDetail(detailID);
    }


    @RequestMapping(value = "/excel", method = RequestMethod.POST)
    public void exportExcel(@RequestBody UserCommissionDto selectDto, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String excelName = ProConstants.EXPORT_COMMISSION_DETAIL + "_" + CommonUtils.getCurTimestamp();
        response.setCharacterEncoding("UTF-8");
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("code", "EXPORTORDERDETAIL");
        map.put("columns", "[{'header':'姓名','field':'userName'},{'header':'订单金额','field':'orderAmount'},{'header':'奖金金额','field':'commission'},{'header':'时间','field':'handleTime'},{'header':'银行卡号','field':'cardNO'},{'header':'持卡人','field':'cardHolderName'},{'header':'开户行','field':'cardBankName'}]");
        switch (map.get("code")) {
            case ProConstants.EXPORT_ORDER_DETAIL:
                list = commissionUserService.exportDetail(map, selectDto, request, response);
                break;
        }
        ExportExcel.excelWrite(map, list, excelName, request, response);
    }


}
